$ ()->
  initRequiredInputs()
  initPhotoList()
  initDatepicker()
  initAutocomplete()
  
initRequiredInputs = (expr)->
  expr = 'input' unless expr?
  $(expr).each ()->
    @oninvalid = (e)->
      unless e.target.validity.valid
        e.target.setCustomValidity 'Ovo polje je obavezno'
    
    @oninput = (e)->
      e.target.setCustomValidity ""
  
initPhotoList = (expr)->
  expr = '.photo-list' unless expr?
  $(expr).sortable
    revert: 'invalid'
    items:  'li:not(.control)'
    cursor: 'move'
    stop: (e, ui)->
      $(@).children('li:not(.control)').each (index)->
        $(@).find('input[type=hidden].position').val index

initDatepicker = (expr)->
  expr = '.datepicker' unless expr?
  $(expr).datepicker()

initAutocomplete = (expr)->
  expr = '.autocomplete' unless expr?
  $(expr).each ()->
    url = $(@).attr 'ac:url'
    onSelect = $(@).attr 'ac:select'
    onSearch = $(@).attr 'ac:search'
    min = parseInt $(@).attr('ac:minLength')
    min = if min>0 then min else 2
    
    $(@).autocomplete
      source:    url
      minLength: min
      change: (e, ui)->
        data = $(@).siblings('input[type=text]').data 'item'
        return unless data?

        $(@).siblings('input[type=hidden]').eq(0).val data.id
        $(@).siblings('input[type=text]').val data.label
      
      select: (e, ui)->
        $(@).siblings('input[type=hidden]').eq(0).val ui.item.id
        $(@).siblings('input[type=text].ui-autocomplete-input').data 'item', ui.item
        
        if onSelect?
          eval onSelect + '(this);'
      
      response: (e, ui)->
        $(@).siblings('input[type=hidden]').eq(0).val ''
        if onSearch?
          eval(onSearch + '(this);')

addPhoto = (a)->
  a = $ a
  wrap = a.parent()
  li   = wrap.parent()
  
  newLi = li.clone true
  newLi.attr 'class', 'picker'
  li.after newLi
  
  btnRemove = newLi.find '.remove'
  btnRemove
    .removeAttr 'onclick'
    .click ()->
      $(@).parent().parent().remove()
  
  btnAdd = newLi.find '.add'
  btnAdd
    .removeAttr 'onclick'
    .attr 'class', 'pick'
    .html '?'
    .click ()->
      $(@).siblings('input[type=file]').click()
  
  inptFile = newLi.find 'input[type=file]'
  inptFile
    .change ()->
      wrap = $(@).parent()
      wrap
        .addClass 'selected'
        .attr 'title', @value
  

pickPhoto = (a)->
  a     = $ a
  wrap  = a.parent()
  file  = wrap.children 'input[type=file]'
  label = wrap.children '.file-name'
  
  file.focus().click()

parse_date_period = (start, end)->
  s = parse_date start
  e = parse_date end
  
  ret = s.day
  if e.month != s.month
    ret += "." + s.monthName

  ret += " - " + e.day + "." + e.monthName
  
  return ret

parse_date = (date)->
  _d = new Date date
  
  m = _d.getMonth()+1; m = if m>9 then m else '0'+m
  d = _d.getDate();    d = if d>9 then d else '0'+d
  
  return {
    month: m
    day:   d
    year:  _d.getFullYear()
    dayName:   locale_sr.dayNames[_d.getDay()]
    monthName: locale_sr.monthNames[_d.getMonth()]
  }
  
currency_format = (number)->
  ret = ''
  numf = parseFloat(number)
  num = numf.toString()
  
  dec = '00'
  pos = num.indexOf "."
  if pos >= 0
    dec = Math.round(parseInt(num.substr(pos+1))).toString().substr(0, 2)
    num = num.substr 0, pos
  
  tmp = num
  loop
    ret = "." + tmp.substr(tmp.length-3) + ret
    
    tmp = tmp.substr(0, tmp.length-3)
    
    break if tmp.length<3
  
  if tmp.length
    ret = tmp + "." + ret.replace /^\./, ''
  
  return ret.replace(/^\.+/, '') + "," + dec

pdfPonuda = (id)->
    win = window.open '/offers/pdf/' + id, 'pdfPonuda', 'width=1000, height=600, scrollbars=1'
    win.focus()

pdfFaktura = (id)->
    win = window.open '/invoices/pdf/' + id, 'pdfFaktura', 'width=800, height=800, scrollbars=1'
    win.focus()